from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.action_chains import ActionChains

# option = Options()
# option.add_experimental_option('excludeSwitches', ['enable-automation'])
# option.add_argument('--disable-blink-features=AutomationControlled')


def get_train(num, from_station, to_station, date):

    # 去除浏览器识别
    option = ChromeOptions()
    option.add_experimental_option('excludeSwitches', ['enable-automation'])
    option.add_experimental_option("detach", True)

    web = Chrome(options=option)

    web.maximize_window()
    web.get('https://kyfw.12306.cn/otn/resources/login.html')

    script = 'Object.defineProperty(navigator, "webdriver", {get: () => false,});'
    web.execute_script(script)      # 允许滑动


    web.implicitly_wait(10)
    web.find_element(By.CSS_SELECTOR, '#J-userName').send_keys('17713995975')   # 输入账号
    web.find_element(By.CSS_SELECTOR, '#J-password').send_keys('205714wp')      # 输入密码
    web.find_element(By.CSS_SELECTOR, '#J-login').click()       # 点击登录
    time.sleep(2)
    # 滑块
    span = web.find_element(By.CSS_SELECTOR, '#nc_1_n1z')
    actioms = ActionChains(web)
    time.sleep(2)
    actioms.click_and_hold(span).move_by_offset(300, 0).perform()


    web.implicitly_wait(10)
    # web.find_element(By.CSS_SELECTOR, '#link_for_ticket').send_keys(Keys.ENTER)     # 回车取消
    web.find_element(By.CSS_SELECTOR, '.modal-ft .btn').click()
    web.find_element(By.CSS_SELECTOR, '#link_for_ticket').click()       # 点击预定
    web.implicitly_wait(10)

    # time.sleep(10)
    # web.find_element(By.CSS_SELECTOR, '#content_defaultwarningAlert_idr').clear()
    # web.find_element(By.CSS_SELECTOR, '#qd_closeDefaultWarningWindowDialog_id').clear()

    web.find_element(By.CSS_SELECTOR, '#fromStationText').send_keys(Keys.ENTER)
    # 输入出发地
    web.find_element(By.CSS_SELECTOR, '#fromStationText').clear()
    web.find_element(By.CSS_SELECTOR, '#fromStationText').click()
    web.find_element(By.CSS_SELECTOR, '#fromStationText').send_keys(from_station)
    web.find_element(By.CSS_SELECTOR, '#fromStationText').send_keys(Keys.ENTER)
    # 输入目的地
    web.find_element(By.CSS_SELECTOR, '#toStationText').clear()
    web.find_element(By.CSS_SELECTOR, '#toStationText').click()
    web.find_element(By.CSS_SELECTOR, '#toStationText').send_keys(to_station)
    web.find_element(By.CSS_SELECTOR, '#toStationText').send_keys(Keys.ENTER)
    # 输入日期
    web.find_element(By.CSS_SELECTOR, '#train_date').clear()   # 清空
    web.find_element(By.CSS_SELECTOR, '#train_date').click()   # 点击
    web.find_element(By.CSS_SELECTOR, '#train_date').send_keys(date)
    web.find_element(By.CSS_SELECTOR, '#train_date').send_keys(Keys.ENTER)     # 回车确定
    # 点击查询
    web.find_element(By.CSS_SELECTOR, '#query_ticket').click()  # 点击

    # if num % 2 == 1:
    #     # 预定票
    #     web.find_element(By.CSS_SELECTOR, f'#queryLeftTable tr:nth-child({num * 2 - 1}) .btn72').click()
    # elif num % 2 == 0:
    #     web.find_element(By.CSS_SELECTOR, f'#queryLeftTable tr:nth-child({num * 2 - 1}) .btn72').click()

    web.find_element(By.CSS_SELECTOR, f'#queryLeftTable tr:nth-child({num * 2 - 1}) .btn72').click()

    # 乘车人
    web.find_element(By.CSS_SELECTOR, '#normalPassenger_0').click()
    # 提交订单
    web.find_element(By.CSS_SELECTOR, '#submitOrder_id').click()
    # 选位置 A
    web.find_element(By.CSS_SELECTOR, '#erdeng1 > ul:nth-child(2) li:nth-child(1) a').click()
    # web.find_element(By.CSS_SELECTOR, fr'#\31 {zuo}')
    web.implicitly_wait(10)
    # 点击确认
    web.find_element(By.XPATH, '//*[@id="qr_submit_id"]').click()
    # time.sleep(100)


    # web.quit()